package com.diandireader.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diandireader.activity.R;
import com.diandireader.domain.BookMarketLoadCategory;
import com.diandireader.domain.BookVO;
import com.diandireader.net.BookMarketClient;
/**
 * the book market
 * @author mayh
 *
 */
public class BookMarketActivity extends Activity {

	private RelativeLayout bestbookLayout;
	private FrameLayout bookMarketContentLayout;
	private ProgressBar progressBar;
	private ExecutorService executorService;
	private static final int THREAD_POOL_SIZE = 6; 
	private BookMarketLoadCategory loadCategory;
	private Handler handler;
	private List<BookVO> bestBookList;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//      remove the title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.reader_main_market);
		handler = new BookMarkHandler();
		setViews();
		setListeners();
		progressBar.setVisibility(View.VISIBLE);
		executorService.submit(new BestBookThread());
	}

	//set views
	public void setViews(){
		bestbookLayout = (RelativeLayout)findViewById(R.id.bestbookLayout);
		bookMarketContentLayout = (FrameLayout)findViewById(R.id.bookmarketframelayout);
		progressBar = (ProgressBar)findViewById(R.id.marketProgressBar);
		executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
	}
	
	//set listeners
	public void setListeners(){
		//click to list the best book
		bestbookLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
			}
		});
	}
	
	//thread 
	class BestBookThread implements Runnable {
		public void run() {
			bestBookList = new ArrayList<BookVO>();
			String data = BookMarketClient.loadData(BookMarketClient.BASEURL+BookMarketClient.BESTBOOK);
			if("ERROR".equals(data)){
				handler.sendEmptyMessage(BookMarketClient.ERROR);
			}else if("TIMEOUT".equals(data)){
				handler.sendEmptyMessage(BookMarketClient.TIMEOUT);
			}else {
				try {
					JSONArray array = new JSONArray(data);
					for(int i=0;i<array.length();i++){
						JSONObject obj = array.getJSONObject(i);
						BookVO bv = new BookVO();
						bv.setName(obj.getString("name"));
						bestBookList.add(bv);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				handler.sendEmptyMessage(BookMarketClient.SUCCESS_BESTBOOK);
			}
		}
	}
	
	//handler to update ui
	class BookMarkHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case BookMarketClient.ERROR : {
				progressBar.setVisibility(View.GONE);
				Toast.makeText(BookMarketActivity.this, "数据请求异常", Toast.LENGTH_SHORT).show();
			}
			break;
			case BookMarketClient.TIMEOUT : {
				progressBar.setVisibility(View.GONE);
				Toast.makeText(BookMarketActivity.this, "服务器连接超时", Toast.LENGTH_SHORT).show();
			}
			break;
			case BookMarketClient.SUCCESS_BESTBOOK : {
				progressBar.setVisibility(View.GONE);
				setBestBookview();
			}
			break;
			}
		}
		
	}
	//set best book view 
	public void setBestBookview(){
		ListView listView = new ListView(BookMarketActivity.this);
		AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		listView.setLayoutParams(params);
		listView.setCacheColorHint(Color.parseColor("#00000000"));
		listView.setAdapter(new BaseAdapter() {
			@Override
			public View getView(int position, View view, ViewGroup parent) {
				BookVO bv = bestBookList.get(position);
				view = View.inflate(BookMarketActivity.this, R.layout.bestbooklistitem, null);
				((TextView)view.findViewById(R.id.bookname)).setText(bv.getName());
				return view;
			}
			
			@Override
			public long getItemId(int position) {
				
				return position;
			}
			
			@Override
			public Object getItem(int position) {
				
				return bestBookList.get(position);
			}
			
			@Override
			public int getCount() {
				
				return bestBookList.size();
			}
		});
		bookMarketContentLayout.removeAllViews();
		bookMarketContentLayout.addView(listView);
	}
}
